package uebung03_w;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

import tools.ColorConversions;

public class BilerTester {
	public static void main(String[] args) throws Exception {
		final BufferedImage bi = ImageIO.read(BilerTester.class.getClassLoader().getResourceAsStream("res/checker.png"));
		final int w = bi.getWidth(), h = bi.getHeight();
		final BufferedImage b2 = new BufferedImage(w*100, h*100, BufferedImage.TYPE_INT_ARGB);
		
		int[] grey = ColorConversions.getFastLuminance(bi.getRGB(0, 0, w, h, new int[w*h], 0, w), new int[w*h]);
		int[] grey2 = new int[w*h*100*100];
		
		for (int j = 0; j<h/2; j++) {
			for (int i = 0; i<w/2; i++) {
				grey[i+j*h] = 255;
			}
		}
		for (int j = 0; j<(h)*100; j++) {
			for (int i = 0; i<(w)*100; i++) {
//				System.out.println(i/10.+" "+j/10.);
				grey2[i+j*w*100] = IterativeKLT.bilerp(grey, i/100., j/100., w, h);
//				grey2[i+j*w*10] = (int)IterativeKLT.subpixel(grey, i/10., j/10., w);

			}
		}
		
		int[] pixels = ColorConversions.buildRGB(grey, grey, grey, new int[w*h]);
		int[] pixels2 = ColorConversions.buildRGB(grey2, grey2, grey2, new int[w*h*100*100]);
		
		bi.setRGB(0, 0, w, h, pixels, 0, w);
		b2.setRGB(0, 0, w*100, h*100, pixels2, 0, w*100);
		
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JComponent canvas = new JComponent() {
			{setPreferredSize(new Dimension(w*100, h*100));}
			@Override
			public void paint(Graphics g) {
				g.drawImage(bi.getScaledInstance(w*100, h*100, BufferedImage.SCALE_FAST), 0, 0, this);
				super.paint(g);
			}
		};
		JComponent canvas2 = new JComponent() {
			{setPreferredSize(new Dimension(w*100, h*100));}
			@Override
			public void paint(Graphics g) {
				g.drawImage(b2, 0, 0, this);
				super.paint(g);
			}
		};
		
		ff.add(canvas, BorderLayout.CENTER);
		ff.add(canvas2, BorderLayout.EAST);
		ff.pack();
		ff.setVisible(true);
	}
}
